﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITD_VAPCF.NganQuy.BLL;
namespace ITD_VAPCF.NganQuy.GUI
{
    public partial class frmDoiTienGiaoDich : Form
    {
        int Count = 0;
        bool Loaded = false;
        public frmDoiTienGiaoDich()
        {
            InitializeComponent();
            dtpTuNgay.Value = Sys_Infor_SectionWorking.g_WorkingDate;
            dtpToiNgay.Value = Sys_Infor_SectionWorking.g_WorkingDate;

            LoadAllGiaoDich();

        }

        DataTable dt;
        private void LoadAllGiaoDich()
        {
            dt = new DataTable();
            dt = NgayQuyBLL.DanhSachGiaoDich_DoiTien(dtpTuNgay.Value.ToString("yyyyMMdd"), dtpToiNgay.Value.ToString("yyyyMMdd"));
            dtgvBangKe.DataSource = dt.DefaultView;
            FilterData();
        }

        private void FilterData()
        {
            if (dt == null) return;
            string _Filter = "(1=1)";

            dt.DefaultView.RowFilter = _Filter;
            dtgvBangKe.Refresh();
        }
        private void dtgvBangKe_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

        }

        private void btnLamMoiDuLieu_Click(object sender, EventArgs e)
        {
            LoadAllGiaoDich();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgvBangKe_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (dtgvBangKe.CurrentRow == null) return;
                if (dtgvBangKe.Rows.Count == 0) return;
                string ID_Giao_Dich = dtgvBangKe.CurrentRow.Cells["GiaoDich_ID"].Value.ToString().Trim();
                frmKKTM_DoiTien frm = new frmKKTM_DoiTien();

                frm.GiaoDichID = ID_Giao_Dich;
                frm.EntityGiaoDich = new ITD_VAPCF.NganQuy.ENTITY.TienMatGiaoDich
                                    (ID_Giao_Dich
                                    , dtgvBangKe.CurrentRow.Cells["loai_giao_dich_id"].Value.ToString()
                                    , ""
                                    , dtgvBangKe.CurrentRow.Cells["SoCT"].Value.ToString()
                                    , Common.ConvertData.ToDouble(dtgvBangKe.CurrentRow.Cells["SoTien"].Value.ToString())
                                    , Common.ConvertData.ToDate(dtgvBangKe.CurrentRow.Cells["NgayGD"].Value.ToString())
                                    );
                frm.EntityKhachHang = new ITD_VAPCF.NganQuy.ENTITY.TTKhachHang("", dtgvBangKe.CurrentRow.Cells["TenKH"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["DiaChi"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["DienThoai"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["DienGiai"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["CMT_Serial"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["CMT_NoiCap"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["CMT_NgayCap"].Value.ToString());

                frm.SumThu = Common.ConvertData.ToDouble(dtgvBangKe.CurrentRow.Cells["SoTien"].Value.ToString());
                frm.ShowDialog();
                LoadAllGiaoDich();
            }
            catch (Exception ex)
            {

            }
        }

        private void frmDuyeGiaoDich_Shown(object sender, EventArgs e)
        {
            Loaded = true;
        }

        private void dtpTuNgay_ValueChanged(object sender, EventArgs e)
        {
            if (Loaded)
                LoadAllGiaoDich();
        }

        private void txtSoCT_Click(object sender, EventArgs e)
        {
            FilterData();
        }

        private void cbTrangThaiGiaoDich_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Loaded) FilterData();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmKKTM_DoiTien frm = new frmKKTM_DoiTien();
            frm.ShowDialog();
            LoadAllGiaoDich();
        }

        private void btnKKTM_Click(object sender, EventArgs e)
        {
            dtgvBangKe_DoubleClick(null, null);
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            try
            {
                if (dtgvBangKe.CurrentRow == null) return;
                if (dtgvBangKe.Rows.Count == 0) return;
                string ID_Giao_Dich = dtgvBangKe.CurrentRow.Cells["GiaoDich_ID"].Value.ToString().Trim();
                frmKKTM_DoiTien frm = new frmKKTM_DoiTien();

                frm.GiaoDichID = ID_Giao_Dich;
                frm.EntityGiaoDich = new ITD_VAPCF.NganQuy.ENTITY.TienMatGiaoDich
                                    (ID_Giao_Dich
                                    , dtgvBangKe.CurrentRow.Cells["loai_giao_dich_id"].Value.ToString()
                                    , ""
                                    , dtgvBangKe.CurrentRow.Cells["SoCT"].Value.ToString()
                                    , Common.ConvertData.ToDouble(dtgvBangKe.CurrentRow.Cells["SoTien"].Value.ToString())
                                    , Common.ConvertData.ToDate(dtgvBangKe.CurrentRow.Cells["NgayGD"].Value.ToString())
                                    );
                frm.EntityKhachHang = new ITD_VAPCF.NganQuy.ENTITY.TTKhachHang("", dtgvBangKe.CurrentRow.Cells["TenKH"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["DiaChi"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["DienThoai"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["DienGiai"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["CMT_Serial"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["CMT_NoiCap"].Value.ToString(),
                                        dtgvBangKe.CurrentRow.Cells["CMT_NgayCap"].Value.ToString());

                frm.SumThu = Common.ConvertData.ToDouble(dtgvBangKe.CurrentRow.Cells["SoTien"].Value.ToString());
                frm.InChungTu();

            }
            catch (Exception ex)
            {

            }
        }

        private void sửaGiaoDịchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dtgvBangKe_DoubleClick(null, null);
        }

        private void inBảngKêToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnIn_Click(null, null);
        }

        private void thêmMớiGiaoDịchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnThem_Click(null, null);
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoaGiaoDich_Click(object sender, EventArgs e)
        {
            if (dtgvBangKe.Rows.Count == 0) return;
            if (MessageBox.Show("Bạn có thực sự muốn xóa giao dịch được chọn?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            dtgvBangKe.EndEdit();
            int Count = dtgvBangKe.Rows.Count - 1;
            for (int i = dtgvBangKe.Rows.Count - 1; i >= 0; i--)
            {
                if (Common.ConvertData.ToInt(dtgvBangKe["Chon", i].Value) == 1)
                {
                    string ID_Giao_Dich = dtgvBangKe["GiaoDich_ID", i].Value.ToString().Trim();
                    KKTienMatBLL objNQ_GiaoDich = new KKTienMatBLL(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
                    objNQ_GiaoDich.Delete(ID_Giao_Dich);
                    objNQ_GiaoDich.Delete_ThongTinKhachHang(ID_Giao_Dich);
                    dt.DefaultView.Delete(i);
                }
            }

        }

        private void btnCheckAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dtgvBangKe.Rows.Count; i++)
            {
                dtgvBangKe["Chon", i].Value = 1;
            }
            dtgvBangKe.EndEdit();
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dtgvBangKe.Rows.Count == 0) return;
            if (MessageBox.Show("Bạn có thực sự muốn xóa giao dịch được chọn?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            dtgvBangKe.EndEdit();

            string ID_Giao_Dich = dtgvBangKe.CurrentRow.Cells["GiaoDich_ID"].Value.ToString().Trim();
            KKTienMatBLL objNQ_GiaoDich = new KKTienMatBLL(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
            objNQ_GiaoDich.Delete(ID_Giao_Dich);
            objNQ_GiaoDich.Delete_ThongTinKhachHang(ID_Giao_Dich);
            dt.DefaultView.Delete(dtgvBangKe.CurrentRow.Index);
        }


    }
}
